﻿/**
 * 插入视频
 */
(function(E, $){
var Consts = E.consts;

E.plugin("Video", {
	config:{
		width:350,
		position: 'center',
		overlay: true
	},
	
	settings : {
		//插入内容模版
		flash_mod : '<embed src="[url]" quality="high" width="[width]" height="[height]" align="middle" allowScriptAccess="always" allowFullScreen="true" mode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" ></embed>',
		url_max_len : 500,
		//url的name属性值
		url_name : 'url_name',
		width_name : 'width_name',
		height_name : 'height_name',
		//地址合法性校验正则
		url_tips : '文件网络地址：',
		tips : '支持.swf文件格式',
		size_tips : '文件显示大小：',
		width_tips : '宽度',
		height_tips : '高度',
		default_url : 'http://',
		default_width : 480,
		default_height : 400,
		max_wid_len : 3,
		max_hei_len : 3
	} ,
	init:function(){
		var settings = this.settings ;
		$.extend(true, settings, this._settings);
		this.cache['html']='<table border="0" width="100%">\
			<tr><td colspan="2"><strong>'+ settings.url_tips+ '</strong></td></tr>\
			<tr><td colspan="2"><input type="text" name="'+ settings.url_name+ '" maxlength="'+ settings.url_max_len+ '" value="'+ settings.default_url+ '" style="width:280px;"></td></tr>\
			<tr><td colspan="2" style="color:#999999;">'+ settings.tips+ '</td></tr>\
			<tr><td colspan="2"><strong>'+ settings.size_tips+ '</strong></td></tr>\
			<tr class="input_tr"><td>' + settings.width_tips + ' <input type="text" name="'+ settings.width_name+ '" value="'+ settings.default_width+'" maxlength="'+ settings.max_wid_len+ '" style="width:80px;"></td>\
			<td>'+ settings.height_tips+ ' <input type="text" name="'+ settings.height_name+ '" value="'+ settings.default_height+ '" maxlength="'+ settings.max_hei_len+ '" style="width:80px;"></td></tr>\
			</table>';
	},
	one : function(panel, self) {
		$.extend(true,check_data,self._check_data);
	},
	
	// 点击确定按钮后的验证方法
	check: function(panel, self){
		var settings = self.settings,
			values = self.getValues(),
			url = values[settings.url_name],
			width = values[settings.width_name],
			height = values[settings.height_name];
		
		var check_msg = check_data.url(url) || check_data.width(width) || check_data.height(height);
		if(check_msg){
			self.error( check_msg );
			return false;
		}
		return true;
	},
	
	get: function(panel, self){
		var settings = self.settings,
			values = self.getValues(),
			url = values[settings.url_name],
			width = values[settings.width_name],
			height = values[settings.height_name];
			
		return settings.flash_mod.replace('[width]', width).replace('[height]', height).replace("[url]", url);
	},
	onclose : function(panel, self) {
		var settings = self.settings ;
	},
	//供外部修改check_data对象属性的接口
	_check_data :{},
	//供外部修改settings对象属性的接口
	_settings : {}
});


var check_data = {
	settings : {
		//宽度最小值
		min_width : 200,
		//高度最小值
		min_height : 200,
		url_tips : '请输入一个有效的视频文件地址！' ,
		url_tips1 : '您输入了违禁词或者非法内容，请修改后发布！',
		wid_tips : '宽度必须是一个大于 200 的整数。',
		hei_tips : '高度必须是一个大于 200 的整数。'
	},
	url : function(val) {
		var settings = this.settings,reg = /(.*)\.(swf)$/ ,
			video_urls = E.get('option').video_whiteList,
			tmp = null, item = '', url_check = false, reg_url = null;

		if(!val || val.length <1)return settings.url_tips;
		if(! reg.test(val) || val.replace(".swf","").length==0)return settings.url_tips;
		
		if(video_urls instanceof Array){
			video_urls = video_urls.join('|').toUpperCase().replace(/http:\/\//ig, '').replace(/\./g,'\\\.')
			video_urls = '\\\.('+ video_urls + ')$';
			reg_url = new RegExp(video_urls,'i');
			val = val.replace('http://','').replace(/(\?|#)/g,'/').split('/');
			if(val && val.length >0){
				val[0] = val[0].charAt(0) === '.' ? val[0] : '.'+val[0];
				if(!reg_url.test(val[0])){
					return settings.url_tips1;
				}
			}else{
				return settings.url_tips1;
			}
		}
		return null;
	},
	width : function(val) {
		var settings = this.settings;
		if(/^[2-9]\d{2}$/.test(val)) {
			return null ;
		}else{
			return settings.wid_tips ;
		}
	},
	height : function(val) {
		var settings = this.settings;
		if(/^[2-9]\d{2}$/.test(val)) {
			return null ;
		}else{
			return settings.hei_tips ;
		}
	}
}

})(jQEditor, jQuery);